package pl.edu.pw.perg.compinfodb.datamodel;

import java.util.List;

import pl.edu.pw.perg.compinfodb.entities.HistoryItem;
import pl.edu.pw.perg.compinfodb.entities.PhysicalElement;
import pl.edu.pw.perg.compinfodb.entities.StructureHistoryItem;
import pl.edu.pw.perg.compinfodb.entities.VirtualElement;
import pl.edu.pw.perg.compinfodb.manager.IHistoryManager;

public class StructureHistoryTableModel extends HistoryTableModel {

	private VirtualElement virtualElement;
	
	
	public StructureHistoryTableModel(IHistoryManager historyManager) {
		super(historyManager);

	}

	private void setVirtualElement(VirtualElement virtualElement) {
		this.virtualElement = virtualElement;
		history.clear();
		history.addAll(historyManager.findVirtualElementHistory(virtualElement));

	}

	public void selectionChanged(VirtualElement ve) {
		setVirtualElement(ve);
		reloadTable();

	}

	public void selectionChanged(PhysicalElement pe) {
//		do nothing
	}

	@Override
	protected List<HistoryItem> loadItems() {
		return historyManager.findVirtualElementHistory(virtualElement);
	}

	@Override
	protected HistoryItem createItem() {
		if(virtualElement == null)
			return null;
		StructureHistoryItem event = new StructureHistoryItem();
		event.setVirtualElement(virtualElement);
		return event;
	}

	public void ejected(PhysicalElement pe){
		createEvent("Ejected module: " + pe.getLabel());
		save();
		refresh();
	}
	
	public void inserted(PhysicalElement pe){
		createEvent("Inserted module: " + pe.getLabel());
		save();
		refresh();
	}
}
